package es.unex.meigas.dataObjects;

import java.util.ArrayList;

public abstract class AbstractInputFactory implements IInputFactory{

	public static final int SHAPE_TYPE_ANY = -1;

	protected IDataObject[] m_Objects;

	public void clearDataObjects() {

		m_Objects = null;

	}

	public void addDataObject(IDataObject obj){

		IDataObject[] newObjects = new IDataObject[m_Objects.length + 1];
		System.arraycopy(m_Objects, 0, newObjects, 0, m_Objects.length);
		newObjects[m_Objects.length] = obj;
		m_Objects = newObjects;

	}

	public RasterLayerAndBand[] getBands() {

		ArrayList list = new ArrayList();

		IRasterLayer[] layers = getRasterLayers();

		for (int i = 0; i < layers.length; i++) {
			for (int j = 0; j < layers[i].getBandsCount(); j++) {
				RasterLayerAndBand rab = new RasterLayerAndBand(layers[i], j);
				list.add(rab);
			}
		}

		RasterLayerAndBand[] bands = new RasterLayerAndBand[list.size()];
		for (int i = 0; i < list.size(); i++) {
			bands[i] = (RasterLayerAndBand) list.get(i);
		}

		return bands;

	}

	public ILayer[] getLayers() {

		ArrayList list = new ArrayList();

		Object[] objs = getDataObjects();

		for (int i = 0; i < objs.length; i++) {
			if (objs[i] instanceof ILayer){
				list.add(objs[i]);
			}
		}

		ILayer[] layers = new ILayer[list.size()];
		for (int i = 0; i < layers.length; i++) {
			layers[i] = (ILayer) list.get(i);
		}

		return layers;

	}

	public IRasterLayer[] getRasterLayers() {

		ArrayList list = new ArrayList();

		Object[] objs = getDataObjects();

		for (int i = 0; i < objs.length; i++) {
			if (objs[i] instanceof IRasterLayer){
				list.add(objs[i]);
			}
		}

		IRasterLayer[] layers = new IRasterLayer[list.size()];
		for (int i = 0; i < layers.length; i++) {
			layers[i] = (IRasterLayer) list.get(i);
		}

		return layers;

	}

	public IVectorLayer[] getVectorLayers(int shapeType) {

		ArrayList list = new ArrayList();

		Object[] objs = getDataObjects();

		for (int i = 0; i < objs.length; i++) {
			if (objs[i] instanceof IVectorLayer){
				IVectorLayer layer = (IVectorLayer) objs[i];
				if (layer.getShapeType() != IVectorLayer.SHAPE_TYPE_WRONG){
					if (layer.getShapeType() == shapeType || shapeType == SHAPE_TYPE_ANY){
						list.add(objs[i]);
					}
				}
			}
		}

		IVectorLayer[] layers = new IVectorLayer[list.size()];
		for (int i = 0; i < layers.length; i++) {
			layers[i] = (IVectorLayer) list.get(i);
		}

		return layers;

	}

	public ITable[] getTables() {

		ArrayList list = new ArrayList();

		Object[] objs = getDataObjects();

		for (int i = 0; i < objs.length; i++) {
			if (objs[i] instanceof ITable){
				list.add(objs[i]);
			}
		}

		ITable[] tables = new ITable[list.size()];
		for (int i = 0; i < tables.length; i++) {
			tables[i] = (ITable) list.get(i);
		}

		return tables;

	}

	public IDataObject[] getDataObjects(){

		return m_Objects;

	}

	public IDataObject getInputFromName(String sName){

		for (int i = 0; i < m_Objects.length; i++) {
			if (m_Objects[i].getName().equals(sName)){
				return m_Objects[i];
			}
		}

		return null;

	}

}
